package rabbitmq

import (
	"flag"
	"github.com/streadway/amqp"
	"log"
	"os"
	"strings"
)

var (
	uri = flag.String("uri", "amqp://admin:admin@localhost:5672/admin", "AMQP URI")
)

func Init() {
	flag.Parse()
}

func NewConn() *amqp.Connection {
	conn, err := amqp.Dial(*uri)
	FailOnError(err, "Connection err:")
	return conn
}

func FailOnError(err error, msg string) {
	if err != nil {
		log.Fatalf("%s : %s\n", msg, err)
	}
}

func BodyFrom(args []string) string {
	var s string
	if (len(args) < 2) || os.Args[1] == "" {
		s = "hello"
	} else {
		s = strings.Join(args[1:], " ")
	}
	return s
}

func SeverityFrom(args []string) string {
	var s string
	if (len(args) < 2) || os.Args[1] == "" {
		s = "info"
	} else {
		s = os.Args[1]
	}
	return s
}
